<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单号">
                <a-input allow-clear placeholder="请输入订单号" v-model="queryParam.keyword"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="订单时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm:ss"
                  @change="onChange"
                  @calendarChange="calendarChange"
                  :ranges="ranges"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="订单来源">
                <div class="select">
                  <a-select v-model="queryParam.from" placeholder="请选择">
                    <a-select-option value="-1">全部</a-select-option>
                    <a-select-option value="1">联盟分销</a-select-option>
                    <a-select-option value="2">推荐佣金</a-select-option>
                    <a-select-option value="3">服务商分账</a-select-option>
                    <a-select-option value="4">店铺分销</a-select-option>
                  </a-select>
                  <a-button class="search-item" type="primary" @click="refresh">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card class="top_card" :bordered="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="refresh">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="0"> 未分账 </a-radio-button>
          <a-radio-button value="1"> 已分账 </a-radio-button>
          <a-radio-button value="2"> 分账失败 </a-radio-button>
        </a-radio-group>
      </div>
      <s-table rowKey="id" size="default" :columns="columns" :data="loadData" ref="table">
        <div slot="status" slot-scope="scope, text">
          <a-tooltip>
            <template slot="title" v-if='text.err_code_des'>
              <div>{{ text.err_code_des }}</div>
            </template>
            <div class="status">
              <span :class="scope == 0 ? 'disabled' :scope == 1 ? 'open':'close'"></span>
              <span>{{ scope == 0 ? '未分账' :scope == 1 ?'已到账':'分账失败' }}</span>
            </div>
          </a-tooltip>

        </div>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import moment from 'moment'
import pick from 'lodash.pick'
import { STable } from '@/components'
import { getProfitList } from '@/api/saas/profit'

const STATUS = {
  1: 'PERSONAL_OPENID',
  2: 'MERCHANT_ID',
  3: 'PERSONAL_SUB_OPENID',
}

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '订单号',
    dataIndex: 'order_no',
  },
  {
    title: '账号',
    dataIndex: 'account',
  },
  {
    title: '账户名称',
    dataIndex: 'name',
  },
  {
    title: '比例',
    dataIndex: 'rate'
  },
  {
    title: '金额',
    dataIndex: 'amount',
    sorter:(a,b) => {a.amount - b.amount}
  },
  {
    title: '分账描述',
    dataIndex: 'description',
  },
  {
    title: '状态',
    dataIndex: 'is_pay',
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '来源',
    dataIndex: 'remark',
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
  },
]

export default {
  name: 'ProfitSharingRecord',
  components: {
    STable,
  },
  data() {
    return {
      selectDate: {},
      locale: zhCN,
      ranges: {
        一月内: () => [moment().startOf('day').subtract(1, 'month'), moment()],
        昨天: () => [moment().startOf('day').subtract(1, 'days'), moment().endOf('day').subtract(1, 'days')],
        最近一周: () => [moment().startOf('day').subtract(1, 'weeks'), moment()],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      form: this.$form.createForm(this),
      // 表头
      columns,
      // 权限树
      permissionTree: [],
      modalLoading: false,
      defaultCheckedKeys: [],
      // 查询条件参数
      queryParam: {
        search_type: '1',
        status: '-1',
        dateStart: null,
        dateEnd: null,
        from: '-1',
        type: '1',
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        Object.assign(parameter, this.queryParam)
        return getProfitList(parameter).then((res) => {
          return res.data
        })
      },
    }
  },
  filters: {},
  created() {},
  methods: {
    refresh(row) {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    },
    moment,
    calendarChange(date, dateString) {
      this.selectDate = date[0]
    },
    onChange(date, dateString) {
      this.queryParam.dateStart = dateString[0]
      this.queryParam.dateEnd = dateString[1]
    },
    resetQueryParams() {
      Object.assign(this.queryParam, {
        search_type: '1',
        status: '-1',
        dateStart: null,
        dateEnd: null,
        from: '-1',
        keyword: null,
        type: '1',
      })
      this.$refs.table.refresh(true)
    },
  },
}
</script>

<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
.class_p {
  height: 10px;
  font-size: 12px;
  color: red;
  padding: 6px;
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .close {
    background-color: #ff4400;
  }
  .disabled {
    background-color: #ccc;
  }
  .open {
    background-color: #65c817;
  }
}
</style>
